<?php

class Alipay {
	private $id = '';
	private $key = '';
	private $email  = '';
	
	//初始化
	public function __construct() {
		$this->id    = C('PARTNER_ID');
		$this->key   = C('SECURITY_KEY');
		$this->email = C('SELLER_EMAIL');
	}
	
	//解析函数
	public function __destruct() {
		$this->id    = '';
		$this->key   = '';
		$this->email = '';
	}
	
	//付款
	public function pay($message) {
		require_once("alipay.config.php");
		require_once("lib/alipay_submit.class.php");

        $payment_type = "1";//支付类型，必填，不能修改
        $paymethod = "bankPay";//默认支付方式，必填
        $notify_url = $message['notify_url'];//服务器异步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数
        $return_url = $message['return_url'];//页面跳转同步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
        $seller_email = $this->email;//卖家支付宝帐户，必填
        $out_trade_no = $message['out_trade_no'];//商户订单号，商户网站订单系统中唯一订单号，必填
        $subject = $message['subject'];//订单名称，必填
        $total_fee = $message['total_fee'];//付款金额，必填
        $body = $message['body'];//订单描述
        $defaultbank = $message['defaultbank'];//默认网银,银行简码请参考接口技术文档，必填
        $show_url = $message['show_url'];//商品展示地址，需以http://开头的完整路径，例如：http://www.xxx.com/myorder.html
        $anti_phishing_key = "";//防钓鱼时间戳，若要使用请调用类文件submit中的query_timestamp函数
        $exter_invoke_ip = "";//客户端的IP地址，非局域网的外网IP地址，如：221.0.0.1
		
		//构造要请求的参数数组，无需改动
		$parameter = array(
			"service"           => "create_direct_pay_by_user",
			"partner"           => trim($alipay_config['partner']),
			"payment_type"	    => $payment_type,
			"notify_url"	    => $notify_url,
			"return_url"	    => $return_url,
			"seller_email"	    => $seller_email,
			"out_trade_no"	    => $out_trade_no,
			"subject"	        => $subject,
			"total_fee"	        => $total_fee,
			"body"	            => $body,
			"paymethod"	        => $paymethod,
			"defaultbank"	    => $defaultbank,
			"show_url"	        => $show_url,
			"anti_phishing_key"	=> $anti_phishing_key,
			"exter_invoke_ip"	=> $exter_invoke_ip,
			"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
		);

		//建立请求
		$alipaySubmit = new AlipaySubmit($alipay_config);
		$html_text = $alipaySubmit->buildRequestForm($parameter, "post", "确认");//建立一个HTML表单同时马上发送
		echo $html_text;
	}
	
	//同步通知
	public function return_url() {
		require_once("alipay.config.php");
		require_once("lib/alipay_notify.class.php");
		
		$alipayNotify = new AlipayNotify($alipay_config);
		$verify_result = $alipayNotify->verifyReturn();
		if($verify_result) {//验证成功
			$out_trade_no = $_GET['out_trade_no'];//商户订单号
			if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
				return $out_trade_no;
			}else{
				return $out_trade_no;
			}
		}else{
			return "";
		}
	}
	
	//异步通知
	public function notify_url() {
		require_once("alipay.config.php");
		require_once("lib/alipay_notify.class.php");

		//计算得出通知验证结果
		$alipayNotify = new AlipayNotify($alipay_config);
		$verify_result = $alipayNotify->verifyNotify();
		if($verify_result) {//验证成功
			$out_trade_no = $_POST['out_trade_no'];//商户订单号
			$trade_no = $_POST['trade_no'];//支付宝交易号
			$trade_status = $_POST['trade_status'];//交易状态
			
		    if($_POST['trade_status'] == 'TRADE_FINISHED') {
				return $out_trade_no;
			}else if($_POST['trade_status'] == 'TRADE_SUCCESS') {
				return $out_trade_no;
			}
		}else{
			return "";
		}
	}
	
	
}
?>